/**
 * Copyright (c) 2018-2028, Joffee.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sailmi.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.sailmi.common.controller.BaseController;
import com.sailmi.common.model.auth.AuthInfo;
import com.sailmi.common.mybatis.support.Condition;
import com.sailmi.common.tool.api.R;
import com.sailmi.common.tool.node.INode;
import com.sailmi.common.tool.utils.Func;
import com.sailmi.common.tool.utils.SecureUtil;
import com.sailmi.system.entity.Role;
import com.sailmi.system.service.IRoleService;
import com.sailmi.system.vo.GrantRolesVo;
import com.sailmi.system.vo.RoleVO;
import com.sailmi.system.wrapper.RoleWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * 控制器
 *
 * @author Joffee
 */
@RestController
@RequestMapping("/role")
@Tag(name = "角色", description = "角色管理接口")
public class RoleController extends BaseController {

	private final IRoleService roleService;

	public RoleController(IRoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@Operation(summary = "详情", description = "传入role")
	public R<RoleVO> detail(@RequestParam(name="id") Long id) {
		Role detail = new Role();
		detail.setId(id);
		detail = roleService.getOne(Condition.getQueryWrapper(detail));
		return R.data(RoleWrapper.build().entityVO(detail));
	}

	/**
	 * 列表
	 */
	@GetMapping("/list")
	@Operation(summary = "列表", description = "传入role")
	public R<List<INode>> list(@RequestParam @ModelAttribute Map<String, Object> role, HttpServletRequest request) {
		AuthInfo authInfo = SecureUtil.getAuthInfo();
		QueryWrapper<Role> queryWrapper = Condition.getQueryWrapper(role, Role.class);
		if(authInfo!=null && authInfo.getEnterprise()!=null) {
			queryWrapper.eq("enterprise_id", authInfo.getEnterprise());
		}
		List<Role> list = roleService.list(queryWrapper);
		return R.data(RoleWrapper.build().listNodeVO(list));
	}

	/**
	 * 获取角色树形结构
	 */
	@GetMapping("/tree")
	@Operation(summary = "树形结构", description = "树形结构")
	public R<List<RoleVO>> tree(HttpServletRequest request) {
		AuthInfo authInfo = SecureUtil.getAuthInfo();
		List<RoleVO> tree = roleService.tree(authInfo.getTenantId());
		return R.data(tree);
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@Operation(summary = "新增或修改", description = "传入role")
	public R submit(@Valid @RequestBody @ModelAttribute Role role) {

		if (Func.isEmpty(role.getId())) {
			String enterpriseId= "000000";
			role.setEnterpriseId(Long.valueOf(enterpriseId));
		}
		return R.status(roleService.saveOrUpdate(role));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@Operation(summary = "删除", description = "传入ids")
	public R remove(@Parameter(name = "主键集合", required = true) @RequestParam(name="ids") String ids) {
		return R.status(roleService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 设置菜单权限
	 *
	 * @param grantRolesVo
	 * @return
	 */
	@PostMapping("/grant")
	@Operation(summary = "权限设置", description = "传入roleId集合以及menuId集合")
	public R grant(@RequestBody GrantRolesVo grantRolesVo) {
		System.out.println("Role IDS IS:"+grantRolesVo.getRoleIds());
		System.out.println("Menu IDS IS:"+grantRolesVo.getMenuIds());
		boolean temp = roleService.grant(Func.toLongList(grantRolesVo.getRoleIds()), Func.toLongList(grantRolesVo.getMenuIds()));
		return R.status(temp);
	}

}
